package service.array;

import domain.dto.UserDto;
import domain.entity.Dept;
import domain.entity.Role;
import domain.entity.User;
import domain.vo.Result;
import domain.vo.UserVo;
import service.IDeptService;
import service.IRoleService;
import service.IUserService;




public class UserService extends Baseservice<User,UserDto, UserVo>implements IUserService {


    private final IDeptService deptService;
    private final IRoleService roleService;

    private static IUserService singleton;


    private UserService() {
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        date = new User[DATA_SIZE+1];
        temp = new UserVo[DATA_SIZE];
    }

    public static IUserService getInstance(){
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }
        String rolrName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            rolrName = role.name;
        }
        return new UserVo(user.getId(), user.name, user.username, user.mobile, user.email, user.deptId, deptName, rolrName, user.roleId, user.enabled);
    }
@Override
     User createEntity(UserDto dto, int id) {
        return  new User(id,dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }
@Override
void updateEntity(UserDto dto, User user) {
        user.name = dto.name;
        user.username = dto.username;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
@Override
    public  boolean deptInUse(int deptId){
        boolean  inUse=false;
        for(User user:date){
            if(user!=null&&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
@Override
    public  boolean roleInUse(int roleId){
        boolean  inUse=false;
        for(User user:date){
            if(user!=null&&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }


@Override
    public  Result create(UserDto dto) {

        if (!deptService.existById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        return  super.create(dto);
        }
    @Override
    public  Result update(UserDto dto, int id){
        if (!deptService.existById(dto.deptId)) {
            return Result.failure("更新失败，不存在指定的部门");
        }
        if (!roleService.existById(dto.roleId)) {
            return Result.failure("更新失败，不存在指定的角色");
        }
        return  super.update(dto,id);
    }
@Override
    public Result toggle(int id) {
        if(!existById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        date[id].enabled=!date[id].enabled;
        return Result.success(date[id].enabled?"启用成功":"停用成功",null);
    }
}
